#include "gameobjects.h"

bool str2Bool(string str, bool doprintf)
{
	bool tmpbool;
	if(str == "true"){ tmpbool = true; }else{ tmpbool = false; }

	// Print out what we have, if wanted
	if(doprintf){ printf("%s\n", str.c_str()); }

	// Return the bool
	return tmpbool;
}

int str2Int(string str, bool doprintf)
{
	// Print out what we have, if wanted
	if(doprintf){ printf("%s\n", str.c_str()); }

	// Return the int
	return atoi(str.c_str());
}

float str2Float(string str, bool doprintf)
{
	// Print out what we have, if wanted
	if(doprintf){ printf("%s\n", str.c_str()); }

	// Return the float
	return atof(str.c_str());
}

void handleXMLData(int &var, string xml_value)
{
	
}
void handleXMLData(bool &var, string xml_value)
{
	
}
void handleXMLData(string &var, string xml_value)
{

}

bool loadGameObjects(string file, vector<gameObjects> &currentObjects, sf::RenderWindow &window)
{
	// The Document
	pugi::xml_document doc;

	// Load the XML file
	pugi::xml_parse_result result = doc.load_file(file.c_str());

	if(!result){ printf("Failed to load the XML file from %s!\n", file.c_str()); }
	else{ printf("Loaded XML file from %s\n", file.c_str()); }
	
	pugi::xml_node root = doc.root();
	pugi::xml_attribute attr;

	// Go through the entire document, all children of the root
	// who will be objects, all having their own attributes
	for(pugi::xml_node current = root.first_child(); current; current = current.next_sibling())
	{
		// Get the attribute of the currently selected child
		attr = current.first_attribute();
		// Get what type it is
		string objectType = attr.as_string();

		// If the object type is image
		if(objectType == "image")
		{
			printf("[Image Type object found]\n");
			printf("> Checking for Attributes\n");

			//Create an object of the gameObjects class that
			// we can populate with stuff and push into the
			// vector array
			gameObjects tmpObject;
			
			// Set the Object Type/Category (I should be more consistent..)
			tmpObject.objectCategory = objectType;

			// Now we know what type of object we're dealing with, lets
			// check the children of the currently selected child and
			// it's attributes
			for(pugi::xml_node next = current.first_child(); next; next = next.next_sibling())
			{
				// Grab the attribute and store it into a string
				pugi::xml_attribute curAtt = next.first_attribute();
				string attType = curAtt.as_string();

				// Compare what type of child we're dealing with
				// and what type of data it's holding
				if(attType == "rotating")
				{
					printf("  > Rotating Attribute Found: ");

					tmpObject.rotating = str2Bool(next.child_value(), true);
				}
				else if(attType == "imglocation")
				{
					printf("  > IMGLocation Attribute Found: %s\n", next.child_value());
					tmpObject.imgLocation = next.child_value();
				}
				else if(attType == "xpos")
				{
					printf("  > Xpos Attribute Found: ");
					tmpObject.x = str2Int(next.child_value(), true);
				}
				else if(attType == "ypos")
				{
					printf("  > Ypos Attribute Found: ");
					tmpObject.y = str2Int(next.child_value(), true);
				}
				else if(attType == "solid")
				{
					printf("  > Solid Attribute Found: ");
					tmpObject.solid = str2Bool(next.child_value(), true);
				}
				else if(attType == "visible")
				{
					printf("  > Visible Attribute Found: ");
					tmpObject.visible = str2Bool(next.child_value(), true);
				}
				else if(attType == "rotationMinAngle")
				{
					printf("  > RotationMinAngle Attribute Found: ");
					tmpObject.rotationMinAngle = str2Float(next.child_value(), true);
				}
				else if(attType == "rotationMaxAngle")
				{
					printf("  > RotationMaxAngle Attribute Found: ");
					tmpObject.rotationMaxAngle = str2Float(next.child_value(), true);
				}
			}

			printf("> Pushing newly created Object into Vector Array\n\n");
			// Push the object into the vector array
			currentObjects.push_back(tmpObject);
		}
		else
		{

		}
	}

	return true;
}

/*
// The Document
pugi::xml_document doc;
// Load the XML file
pugi::xml_parse_result result = doc.load_file(goFileLocation.c_str());

if(!result){ printf("Failed to load the XML file!\n"); }else{ printf("Loaded XML file\n"); }
	
pugi::xml_node tools = doc.root();

// While we have siblings, go through all of them
for(pugi::xml_node tool = tools.first_child(); tool; tool = tool.next_sibling())
{
	// Go through the attributes
    for(pugi::xml_attribute attr = tool.first_attribute(); attr; attr = attr.next_attribute())
    {
        printf("%s = %s (%s)\n", attr.name(), attr.as_string(), tool.name());
    }

	// Check if the node has more children or not
	if(tool.first_child())
	{
		printf("This node has more children!\n");
	}
	else
	{
		printf("This node does not have any more children!\n");
	}
}

*/